package com.wpw.dream.spider.service.impl;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpw.dream.spider.dao.ContentDao;
import com.wpw.dream.spider.dao.SpiderConfigDao;
import com.wpw.dream.spider.entity.SpiderConfig;
import com.wpw.dream.spider.enums.SpiderRunStatusEnum;
import com.wpw.dream.spider.processor.ContentPipeline;
import com.wpw.dream.spider.processor.SpiderPageProcessor;
import com.wpw.dream.spider.processor.StopableSpider;
import com.wpw.dream.spider.service.SpiderService;

import us.codecraft.webmagic.scheduler.QueueScheduler;

@Service("spiderService")
public class SpiderServiceImpl implements SpiderService {

	public static final Long CACHE_TIME = 1000 * 60 * 10L;
	
	private Long findTime = 0L;
	
	private List<SpiderConfig> cacheSpiders;
	
	ExecutorService pool = Executors.newFixedThreadPool(5);
	
	@Autowired
	private SpiderConfigDao spiderConfigDao;
	
	@Autowired
	private ContentDao contentDao;
	
	@Override
	public void doSpider(List<Long> spiderIds) {
		
		List<SpiderConfig> list = spiderConfigDao.queryByIds(spiderIds);
		if (CollectionUtils.isNotEmpty(list)) {
			for (SpiderConfig spiderConfig : list) {
				if (SpiderRunStatusEnum.STOP.getValue().equals(spiderConfig.getRunStatus())) {
					spiderConfig.setMaxArticleTime(contentDao.queryMaxArticleTime(spiderConfig.getId()));
					pool.execute(new SpiderRun(spiderConfig));
				}
				
			}
		}
	}
	
	class SpiderRun implements Runnable {

		SpiderConfig spiderConfig;
		
		public SpiderRun(SpiderConfig spiderConfig) {
			this.spiderConfig = spiderConfig;
		}
		
		@Override
		public void run() {
			spiderConfig.setRunStatus(SpiderRunStatusEnum.RUN.getValue());
			spiderConfig.setLastRuntime(System.currentTimeMillis());
			spiderConfigDao.update(spiderConfig);
			
			StopableSpider.create(new SpiderPageProcessor(spiderConfig))
			.addUrl(spiderConfig.getSourceUrl())
//			.setScheduler(new SpiderQueueScheduler(spiderConfig.getMaxArticleTime()))
			.setScheduler(new QueueScheduler())
			.addPipeline(new ContentPipeline())
			.thread(5)
			.run();
			
			spiderConfig.setRunStatus(SpiderRunStatusEnum.STOP.getValue());
			spiderConfigDao.update(spiderConfig);
		}
		
	}

	@Override
	public List<SpiderConfig> findAll() {
		
		List<SpiderConfig> findAll = spiderConfigDao.findAll();
		cacheSpiders = findAll;
		findTime = System.currentTimeMillis();
		return findAll;
	}

	@Override
	public List<SpiderConfig> findAllCache() {
		if (cacheSpiders == null || cacheSpiders.size() == 0 || System.currentTimeMillis() - findTime > CACHE_TIME) {
			findAll();
		} 
		return cacheSpiders;
	}
	
	
}
